package com.spi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spi.mapper.TransMapper;
import com.spi.model.Trans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user/trans")
@RestController
public class TransController {

    @Autowired
    private TransMapper transMapper ;

    @GetMapping("/getCousumerStatusByMessageId/{id}")
    public int getCousumerStatusByMessageId(@PathVariable("id") String messageId) {
        LambdaQueryWrapper<Trans> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Trans::getMessagekey,messageId);
        List<Trans> transList = transMapper.selectList(lambdaQueryWrapper);
        if (transList == null || transList.isEmpty()) {
            throw new RuntimeException("事务消息为做存储但是投递到了消费者端。。");
        }
        return transList.get(0).getConsumerstatus() ;
    }
    @PutMapping("/updateCousumerStatusByMessageId/{messageId}/{status}")
    public int updateCousumerStatusByMessageId(@PathVariable("messageId") String messageId,@PathVariable("status") int status) {
        Trans trans = new Trans() ;
        //trans.setMessagekey(messageId);
        trans.setConsumerstatus(status);
        LambdaQueryWrapper<Trans> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Trans::getMessagekey,messageId);
        int update = transMapper.update(trans, lambdaQueryWrapper);
        if (update == 1){
            return 1;
        }else {
            return 0 ;
        }

    }
}
